#!/usr/bin/python
# -*- coding:utf-8 -*-
# project : python_web
# user :taihe
# Author: 冀恩开
# createtime :2018/8/1 13:48
import io
from python_web.tools import check_code as CheckCode
import json
from base64 import decode
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render, HttpResponse
import logging
# Create your views here.
from python_web.tools.mysql import getDB
from python_web.tools.Output_Helper import *
logger = logging.getLogger('app.views')

def login(request):
    pass


def index(request):
    try:
        logger.info('1111111111')
    except Exception as e:
        logger.error(e)
    return render(request, 'index_1.html')

def probation(request):
    try:
        print('test')
    except Exception as e:
        logger.error(e)
    return render(request, 'login.html')

def subject(request):
    # print(id)
    if request.method == 'GET':
        query = request.GET.get('type', '')
        if int(query) == 1:  # 会员
            return HttpResponse('试用')
        if int(query) == 0:  # 试用
            mysql = getDB()
            sqlAll = "select * from Choose_subjects"
            result, count = mysql.getAll(sqlAll)
            print(result)
            return render(request,'subject.html',{'res':result})
    return render(request, 'index_1.html')


def topic(request,id):
    if request.method == 'GET':
        return render(request, 'topic.html' ,locals())
    return render(request, 'index_1.html')


def select(request):
    if request.method == 'GET':
        subject_id = request.GET.get('subject_id', '')
        mysql = getDB()
        sqlAll = "select * from Choice_question where subject_id=%s" % (subject_id)
        result, count = mysql.getAll(sqlAll)
        return render(request, 'select.html', {'res':result[0],'count':count})
    return render(request, 'index_1.html')

def operation(request):
    if request.method == 'GET':
        subject_id = request.GET.get('subject_id', '')
        mysql = getDB()
        sqlAll = "select * from Operation_problem where subject_id=%s" % (subject_id)
        result, count = mysql.getAll(sqlAll)
        print(result)
        return render(request, 'operation.html', {'res':result,'count':count})
    return render(request, 'index_1.html')

@csrf_exempt
def operation_problem(request,operation_problem):
    mysql = getDB()
    sqlAll = "select * from Operation_problem where Problem_set=%s" % (operation_problem)
    result= mysql.getOne(sqlAll)
    return render(request, 'operation_problem.html', {'res':result})
    # return render(request, 'index_1.html')


@csrf_exempt
def addNext(request):
    ret = ReturnJ()
    mysql = getDB()
    sqlAll = "select * from Choice_question where id=2"
    result = mysql.getOne(sqlAll)
    # a = result[0]['title']
    print(result)
    ret.result = result[0]['title']
    try:
        if request.method == 'POST':
            select_id = request.POST.get('id','')
            print(select_id)
            mysql = getDB()
            sqlAll = "select * from Choice_question where id=%s" % (select_id)
            result = mysql.getOne(sqlAll)
            print(result)
            ret.result = result[0]

            print(result[0])
    except Exception as e:
        print(11111)
        ret.code = 1001
        ret.msg = str(e)
    res = HttpResponse(ret.toJson())
    return res

def check_code(request):
    """
    获取验证码
    :param request:
    :return:
    """
    stream = io.BytesIO()
    # 创建随机字符 code
    # 创建一张图片格式的字符串，将随机字符串写到图片上
    img, code = CheckCode.create_validate_code()
    img.save(stream, "PNG")
    # 将字符串形式的验证码放在Session中
    # request.session["CheckCode"] = code
    return HttpResponse(stream.getvalue())

if __name__ == "__main__":
    a = b'dfa'
    print(type(a))
    print(a)


